const { DataTypes } = require('sequelize')
const sequelize = require('../config/database')

const WorkOrder = sequelize.define('WorkOrder', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  workOrderNo: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true
  },
  productId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'materials',
      key: 'id'
    }
  },
  productName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  planQuantity: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false
  },
  actualQuantity: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0
  },
  unit: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: '件'
  },
  priority: {
    type: DataTypes.ENUM('low', 'medium', 'high', 'urgent'),
    defaultValue: 'medium'
  },
  status: {
    type: DataTypes.ENUM('pending', 'running', 'completed', 'stopped'),
    defaultValue: 'pending'
  },
  processId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'processes',
      key: 'id'
    }
  },
  equipmentId: {
    type: DataTypes.UUID,
    allowNull: true,
    references: {
      model: 'equipment',
      key: 'id'
    }
  },
  startTime: {
    type: DataTypes.DATE,
    allowNull: true
  },
  endTime: {
    type: DataTypes.DATE,
    allowNull: true
  },
  estimatedDuration: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '预计生产时长(分钟)'
  },
  actualDuration: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '实际生产时长(分钟)'
  },
  operator: {
    type: DataTypes.STRING,
    allowNull: true
  },
  qualityCheck: {
    type: DataTypes.BOOLEAN,
    defaultValue: false
  },
  notes: {
    type: DataTypes.TEXT,
    allowNull: true
  }
}, {
  tableName: 'work_orders',
  timestamps: true
})

module.exports = WorkOrder
